﻿
using WCYCReader.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using WCYCReader.Utility;
namespace WCYCReader.ViewModels
{
    public class LocalBookList_VM
    {
        private ObservableCollection<BookList_M> _T_LocalBookList;
        //private BookList_M[] temporary_LocalBookList;

        public void AddBookList(BookList_M lblm_item)
        {
            this.T_LocalBookListViewModel().Count();
            this._T_LocalBookList.Add(lblm_item);
            this.SaveLocalBookList();
        }

        public void DeleteBookList(BookList_M lblm_item)
        {
            int index = 0;
            foreach (BookList_M t_m in this.T_LocalBookListViewModel())
            {
                if (t_m.str_Id_M == lblm_item.str_Id_M)
                {
                    break;
                }
                index++;
            }
            this.T_LocalBookListViewModel().RemoveAt(index);
            this.SaveLocalBookList();
        }


        private void SaveLocalBookList()
        {
            SettingHelper.SaveInToSettings("QQReader_LocalBookList", this.T_LocalBookListViewModel().ToList<BookList_M>());
            if (this.T_LocalBookListViewModel().Count == 0)
            {
                SettingHelper.DeleteSettings("QQReader_LocalBookList");
            }
        }

        public ObservableCollection<BookList_M> T_LocalBookListViewModel()
        {
            if (this._T_LocalBookList == null)
            {
                if (SettingHelper.IsSettings("QQReader_LocalBookList"))
                {
                    this._T_LocalBookList = new ObservableCollection<BookList_M>(SettingHelper.GetValueFromSettings("QQReader_LocalBookList") as List<BookList_M>);
                }
                else
                {
                    this._T_LocalBookList = new ObservableCollection<BookList_M>();
                }
            }
            return this._T_LocalBookList;
        }


        //private ObservableCollection<BookList_M> _T_LocalBookList1;
        //private ObservableCollection<BookList_M> _T_LocalBookList2;
        //private BookList_M[] temporary_LocalBookList;

        //public void AddBookList(BookList_M lblm_item)
        //{
        //    this.temporary_LocalBookList = null;
        //    int count = this.T_LocalBookList_VM1().Count;
        //    this.temporary_LocalBookList = new BookList_M[count];
        //    this.T_LocalBookList_VM1().CopyTo(this.temporary_LocalBookList, 0);
        //    this.T_LocalBookList_VM2().Insert(0, lblm_item);
        //    this.T_LocalBookList_VM1().Clear();
        //    for (int i = 0; i < this.T_LocalBookList_VM2().Count; i++)
        //    {
        //        this.T_LocalBookList_VM1().Add(this.T_LocalBookList_VM2()[i]);
        //    }
        //    this.T_LocalBookList_VM2().Clear();
        //    for (int j = 0; j < count; j++)
        //    {
        //        this.T_LocalBookList_VM2().Add(this.temporary_LocalBookList[j]);
        //    }
        //    this.SaveLocalBookList();
        //}

        //public void DeleteBookList1(BookList_M lblm_item)
        //{
        //    int index = 0;
        //    foreach (BookList_M t_m in this.T_LocalBookList_VM1())
        //    {
        //        if (t_m.str_Id_M == lblm_item.str_Id_M)
        //        {
        //            break;
        //        }
        //        index++;
        //    }
        //    this.T_LocalBookList_VM1().RemoveAt(index);
        //    int count = this.T_LocalBookList_VM1().Count;
        //    int num3 = this.T_LocalBookList_VM2().Count;
        //    List<BookList_M> list = new List<BookList_M>();
        //    List<BookList_M> list2 = new List<BookList_M>();
        //    for (int i = index; i < count; i++)
        //    {
        //        list.Add(this.T_LocalBookList_VM1()[index]);
        //        this.T_LocalBookList_VM1().RemoveAt(index);
        //    }
        //    for (int j = index; j < num3; j++)
        //    {
        //        list2.Add(this.T_LocalBookList_VM2()[index]);
        //        this.T_LocalBookList_VM2().RemoveAt(index);
        //    }
        //    for (int k = 0; k < list2.Count; k++)
        //    {
        //        this.T_LocalBookList_VM1().Add(list2[k]);
        //    }
        //    for (int m = 0; m < list.Count; m++)
        //    {
        //        this.T_LocalBookList_VM2().Add(list[m]);
        //    }
        //    this.SaveLocalBookList();
        //}

        //public void DeleteBookList2(BookList_M lblm_item)
        //{
        //    int index = 0;
        //    foreach (BookList_M t_m in this.T_LocalBookList_VM2())
        //    {
        //        if (t_m.str_Id_M == lblm_item.str_Id_M)
        //        {
        //            break;
        //        }
        //        index++;
        //    }
        //    this.T_LocalBookList_VM2().RemoveAt(index);
        //    int count = this.T_LocalBookList_VM1().Count;
        //    int num3 = this.T_LocalBookList_VM2().Count;
        //    List<BookList_M> list = new List<BookList_M>();
        //    List<BookList_M> list2 = new List<BookList_M>();
        //    for (int i = index + 1; i < count; i++)
        //    {
        //        list.Add(this.T_LocalBookList_VM1()[index + 1]);
        //        this.T_LocalBookList_VM1().RemoveAt(index + 1);
        //    }
        //    for (int j = index; j < num3; j++)
        //    {
        //        list2.Add(this.T_LocalBookList_VM2()[index]);
        //        this.T_LocalBookList_VM2().RemoveAt(index);
        //    }
        //    for (int k = 0; k < list2.Count; k++)
        //    {
        //        this.T_LocalBookList_VM1().Add(list2[k]);
        //    }
        //    for (int m = 0; m < list.Count; m++)
        //    {
        //        this.T_LocalBookList_VM2().Add(list[m]);
        //    }
        //    this.SaveLocalBookList();
        //}

        //private void SaveLocalBookList()
        //{
        //    SettingHelper.SaveInToSettings("QQReader_LocalBookList1", this.T_LocalBookList_VM1().ToList<BookList_M>());
        //    SettingHelper.SaveInToSettings("QQReader_LocalBookList2", this.T_LocalBookList_VM2().ToList<BookList_M>());
        //    if (this.T_LocalBookList_VM1().Count == 0)
        //    {
        //        SettingHelper.DeleteSettings("QQReader_LocalBookList1");
        //    }
        //    if (this.T_LocalBookList_VM2().Count == 0)
        //    {
        //        SettingHelper.DeleteSettings("QQReader_LocalBookList2");
        //    }
        //}

        //public ObservableCollection<BookList_M> T_LocalBookList_VM1()
        //{
        //    if (this._T_LocalBookList1 == null)
        //    {
        //        if (SettingHelper.IsSettings("QQReader_LocalBookList1"))
        //        {
        //            this._T_LocalBookList1 = new ObservableCollection<BookList_M>(SettingHelper.GetValueFromSettings("QQReader_LocalBookList1") as List<BookList_M>);
        //        }
        //        else
        //        {
        //            this._T_LocalBookList1 = new ObservableCollection<BookList_M>();
        //        }
        //    }
        //    return this._T_LocalBookList1;
        //}

        //public ObservableCollection<BookList_M> T_LocalBookList_VM2()
        //{
        //    if (this._T_LocalBookList2 == null)
        //    {
        //        if (SettingHelper.IsSettings("QQReader_LocalBookList2"))
        //        {
        //            this._T_LocalBookList2 = new ObservableCollection<BookList_M>(SettingHelper.GetValueFromSettings("QQReader_LocalBookList2") as List<BookList_M>);
        //        }
        //        else
        //        {
        //            this._T_LocalBookList2 = new ObservableCollection<BookList_M>();
        //        }
        //    }
        //    return this._T_LocalBookList2;
        //}
    }
}

